import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'

const baseUrl = '/v1'
const comUrl = '/authentication'
// const upmsUrl = CommonConfig.upmsUrl + '/v1'
const datacenterUrl = CommonConfig.datacenterUrl + '/v1'
/**
 * 登录
 **/
export function login ({ userName, password, tid }) {
  const newUrl = baseUrl + '/login'
  const params = {
    userName,
    password
  }
  return axiosInstance.post(newUrl, params, {
    headers: {
      'tid': tid
    }
  }).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 *
获取用户所属企业列表
 **/
export function getCompnies (phone) {
  const newUrl = datacenterUrl + `/user_companies?phone=${phone}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 发送验证码
 * phone 手机号
 **/
export function sendCode (phone) {
  const newUrl = '/sms/v1/code'
  const params = {
    phone
  }

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 验证码登录
 * phone 手机号
 * code 验证码
 **/
export function codeLogin (params) {
  const newUrl = baseUrl + '/loginbycode'

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 重置密码
 * phone 手机号
 * newPassword 验证码
 **/
export function resetPassword (params) {
  const newUrl = '/upms/v1/users/password'

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 注销
 **/
export function logout () {
  const newUrl = comUrl + '/logout'

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  }).catch(() => {
    return Promise.reject()
  })
}

/**
 * 刷新token
 * **/
export function refreshToken () {
  const newUrl = baseUrl + '/refresh_token'

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
